<template>
  <el-dialog
    :title="!dataForm.catIdentify ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="编号" prop="catNumber">
      <el-input v-model="dataForm.catNumber" placeholder="编号"></el-input>
    </el-form-item>
    <el-form-item label="名字" prop="catName">
      <el-input v-model="dataForm.catName" placeholder="名字"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="catSex">
      <el-input v-model="dataForm.catSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="照片" prop="catSrc">
      <el-input v-model="dataForm.catSrc" placeholder="照片"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="catBirth">
      <el-input v-model="dataForm.catBirth" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="父亲" prop="catFather">
      <el-input v-model="dataForm.catFather" placeholder="父亲"></el-input>
    </el-form-item>
    <el-form-item label="母亲" prop="catMother">
      <el-input v-model="dataForm.catMother" placeholder="母亲"></el-input>
    </el-form-item>
    <el-form-item label="品种" prop="catType">
      <el-input v-model="dataForm.catType" placeholder="品种"></el-input>
    </el-form-item>
    <el-form-item label="毛色" prop="catColor">
      <el-input v-model="dataForm.catColor" placeholder="毛色"></el-input>
    </el-form-item>
    <el-form-item label="来源" prop="catOrigin">
      <el-input v-model="dataForm.catOrigin" placeholder="来源"></el-input>
    </el-form-item>
    <el-form-item label="子代" prop="catDescendant">
      <el-input v-model="dataForm.catDescendant" placeholder="子代"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          catIdentify: 0,
          catNumber: '',
          catName: '',
          catSex: '',
          catSrc: '',
          catBirth: '',
          catFather: '',
          catMother: '',
          catType: '',
          catColor: '',
          catOrigin: '',
          catDescendant: ''
        },
        dataRule: {
          catNumber: [
            { required: true, message: '编号不能为空', trigger: 'blur' }
          ],
          catName: [
            { required: true, message: '名字不能为空', trigger: 'blur' }
          ],
          catSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          catSrc: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ],
          catBirth: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          catFather: [
            { required: true, message: '父亲不能为空', trigger: 'blur' }
          ],
          catMother: [
            { required: true, message: '母亲不能为空', trigger: 'blur' }
          ],
          catType: [
            { required: true, message: '品种不能为空', trigger: 'blur' }
          ],
          catColor: [
            { required: true, message: '毛色不能为空', trigger: 'blur' }
          ],
          catOrigin: [
            { required: true, message: '来源不能为空', trigger: 'blur' }
          ],
          catDescendant: [
            { required: true, message: '子代不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.catIdentify = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.catIdentify) {
            this.$http({
              url: this.$http.adornUrl(`/cat/basic/info/${this.dataForm.catIdentify}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.catNumber = data.basic.catNumber
                this.dataForm.catName = data.basic.catName
                this.dataForm.catSex = data.basic.catSex
                this.dataForm.catSrc = data.basic.catSrc
                this.dataForm.catBirth = data.basic.catBirth
                this.dataForm.catFather = data.basic.catFather
                this.dataForm.catMother = data.basic.catMother
                this.dataForm.catType = data.basic.catType
                this.dataForm.catColor = data.basic.catColor
                this.dataForm.catOrigin = data.basic.catOrigin
                this.dataForm.catDescendant = data.basic.catDescendant
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cat/basic/${!this.dataForm.catIdentify ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'catIdentify': this.dataForm.catIdentify || undefined,
                'catNumber': this.dataForm.catNumber,
                'catName': this.dataForm.catName,
                'catSex': this.dataForm.catSex,
                'catSrc': this.dataForm.catSrc,
                'catBirth': this.dataForm.catBirth,
                'catFather': this.dataForm.catFather,
                'catMother': this.dataForm.catMother,
                'catType': this.dataForm.catType,
                'catColor': this.dataForm.catColor,
                'catOrigin': this.dataForm.catOrigin,
                'catDescendant': this.dataForm.catDescendant
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
